﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web.UI.WebControls;
using Hematology.Data;
using Hematology.HmtMembership;
using Hematology.Tools;
using Hematology.UserSite.Controls;

namespace Hematology.UserSite
{
	public partial class Default : NgoPage
	{
		protected void Page_Load(object sender, EventArgs e)
		{
			if (!Page.IsPostBack)
			{
				FillCommunityList();
				FillNewsList();
				FillEventsList();
				FillProjectsList();
				FillPartnersList();

				FillCommunityInfoList();
			}
		}

		protected void FillCommunityList()
		{
			List<SiteContent> lCommunity = SiteContent.ListAllContent((int)EnumHelper.ContentType.Community, !MembershipHelper.MembershipService.IsAuthorizedRequest, true);

			if (lCommunity.Count > 5)
				lCommunity.RemoveRange(5, lCommunity.Count - 5);

			rptCommunity.DataSource = lCommunity;
			rptCommunity.DataBind();
		}

		protected void FillNewsList()
		{
			List<SiteContent> lNews = SiteContent.ListAllContent((int)EnumHelper.ContentType.News, !MembershipHelper.MembershipService.IsAuthorizedRequest, true);

			if (lNews.Count > 5)
				lNews.RemoveRange(5, lNews.Count - 5);

			rptNewsList.DataSource = lNews;
			rptNewsList.DataBind();

			rptNews.DataSource = lNews;
			rptNews.DataBind();
		}

		protected void FillEventsList()
		{
			List<SiteEvent> lEvents = new List<SiteEvent>();

			SiteEvent congressEvent = SiteEvent.ListAllEvents((int)EnumHelper.EventType.Congress, !MembershipHelper.MembershipService.IsAuthorizedRequest).FirstOrDefault();
			if (congressEvent != null)
				lEvents.Add(congressEvent);

			SiteEvent conferenceEvent = SiteEvent.ListAllEvents((int)EnumHelper.EventType.Conference, !MembershipHelper.MembershipService.IsAuthorizedRequest).FirstOrDefault();
			if (conferenceEvent != null)
				lEvents.Add(conferenceEvent);

			SiteEvent seminarEvent = SiteEvent.ListAllEvents((int)EnumHelper.EventType.Seminar, !MembershipHelper.MembershipService.IsAuthorizedRequest).FirstOrDefault();
			if (seminarEvent != null)
				lEvents.Add(seminarEvent);

			SiteEvent nosologyEvent = SiteEvent.ListAllEvents((int)EnumHelper.EventType.Nosology, !MembershipHelper.MembershipService.IsAuthorizedRequest).FirstOrDefault();
			if (nosologyEvent != null)
				lEvents.Add(nosologyEvent);

			rptEventsList.DataSource = lEvents;
			rptEventsList.DataBind();
		}

		protected void FillProjectsList()
		{
			List<SiteContent> lProjects = SiteContent.ListAllContent((int)EnumHelper.ContentType.Projects, !MembershipHelper.MembershipService.IsAuthorizedRequest, true);

			if (lProjects.Count > 5)
				lProjects.RemoveRange(5, lProjects.Count - 5);

			rptProjectsList.DataSource = lProjects;
			rptProjectsList.DataBind();

			rptProjects.DataSource = lProjects;
			rptProjects.DataBind();
		}

		protected void FillPartnersList()
		{
			List<SiteContent> lPartners = SiteContent.ListAllContent((int)EnumHelper.ContentType.Partners, !MembershipHelper.MembershipService.IsAuthorizedRequest, true);

			if (lPartners.Count > 5)
				lPartners.RemoveRange(5, lPartners.Count - 5);

			rptPartners.DataSource = lPartners;
			rptPartners.DataBind();
		}

		private void FillCommunityInfoList()
		{
			List<SiteContent> lCommunityInfo = SiteContent.ListAllContent((int)EnumHelper.ContentType.Community, !MembershipHelper.MembershipService.IsAuthorizedRequest, true);

			rptCommunityFull.DataSource = lCommunityInfo;
			rptCommunityFull.DataBind();
		}

		protected void rptContentAttach_ItemCommand(object source, RepeaterCommandEventArgs e)
		{
			if (e.CommandName == "OpenAttach")
			{
				int iAttachId = Convert.ToInt32(e.CommandArgument);
				FileAttach fileAttach = FileAttach.Get(iAttachId);
				if (fileAttach != null)
				{
					Response.Redirect(Path.Combine(ConfigReader.SiteContentAttachmentPath, fileAttach.DocumentName));
				}
			}
		}
	}
}